package com.jinxin.platform.cdfaceacs.controller;

import com.alibaba.fastjson.JSON;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdfaceacs.bean.CdzxkacsUser;
import com.jinxin.platform.cdfaceacs.common.Constant;
import com.jinxin.platform.cdfaceacs.rpc.SysUserRpc;
import com.jinxin.platform.cdfaceacs.service.IAcsUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-26
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/zxkUser")
@Slf4j
public class ZxkUserController {
    @Autowired
    private IAcsUserService userService;
    @Autowired
    private SysUserRpc sysUserRpc;

    @RequestMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam String deviceId,@RequestParam(required = false) String phone,
                                   @RequestParam(required = false) String name){
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("deviceId", deviceId);
        param.put("phone", phone);
        param.put("name", name);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        if(StringUtils.isEmpty(deviceId)){
            resultMap.put("count", 0);
            resultMap.put("data", null);
            return resultMap;
        }
        resultMap.put("count", userService.listPageCount(param));
        resultMap.put("data", userService.listPage(param));
        return resultMap;
    }

    @GetMapping("/listDeviceNames")
    public JsonResult listDeviceNames(String communityId){
        return userService.listDeviceNames(communityId);
    }

    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdzxkacsUser cdzxkacsUser){
        return userService.saveOrUpdate(cdzxkacsUser);
    }

    @PostMapping("/saveBatch")
    public JsonResult saveBatch(@RequestBody CdzxkacsUser cdzxkacsUser){
        if(cdzxkacsUser == null){
            return JsonResult.error("参数错误");
        }
        String unitId = cdzxkacsUser.getUnitId();
        if(StringUtils.isEmpty(unitId)){
            return JsonResult.error("楼栋单元id不存在");
        }
        JsonResult jsonResult = sysUserRpc.loadUserList(unitId);
        if(jsonResult != null){
            List<CdzxkacsUser> userList = JSON.parseArray(toJSONString(jsonResult.getData()), CdzxkacsUser.class);
            int count = 0;
            String communityId = cdzxkacsUser.getCommunityId();
            String deviceId = cdzxkacsUser.getDeviceId();
            String timeTemplate = cdzxkacsUser.getTimeTemplate();
            for(CdzxkacsUser user : userList){
                user.setCommunityId(communityId);
                user.setDeviceId(deviceId);
                user.setTimeTemplate(timeTemplate);
                user.setUnitId(unitId);
                user.setId(null);//新增id为null
                user.setCertificateType(0);//默认身份证
                user.setPersonCode("YG"+System.currentTimeMillis());
                if(StringUtils.isEmpty(user.getPersonType())){
                    user.setPersonType(Constant.USER_TYPE_OWNER);//默认是业主
                }
                try {
                    JsonResult result = userService.saveOrUpdate(user);
                    if(result.getSuccess()){
                        count++;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            int failCount = userList.size() - count;
            return JsonResult.ok("下发单元人员数据:成功"+count+"条,失败"+failCount+"条",null);
        }
        return JsonResult.error("批量下发失败，请重试或联系管理员");
    }

    @GetMapping("/loadBuildingList")
    public JsonResult loadBuildingList(String communityId){
        return sysUserRpc.loadBuildingList(communityId);
    }

    @GetMapping("/loadUserList")
    public JsonResult loadUserList(String unitId){
        return sysUserRpc.loadUserList(unitId);
    }

    @GetMapping("/delete")
    public JsonResult delete(String id,String deviceId,Integer type){
        return userService.delete(id,deviceId,type);
    }

    @PostMapping("/syncData")
    public JsonResult syncData(){
        return null;
    }

    @GetMapping("/loadTemplatesByUser")
    public JsonResult loadTemplatesByUseer(String id){
        return userService.getTemplatesByUserId(id);
    }

    @CrossOrigin
    @GetMapping("/listUserRecord")
    public JsonResult listUserRecord(){
        return userService.listUserRecord();
    }
}
